/***********************************************************************************************************************
    @file    i2c_master.h
    @author  FAE Team
    @date    2-2025
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2025> <MySentech></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/
  
#ifndef _I2C_MASTER_H_
#define _I2C_MASTER_H_
#include "mc1081_reg.h"

#define MY_DELAY_US  Delay_us


/* Definition of GPIOI2C constants */
#define GPIOI2C_ACK  				((unsigned char)1)
#define GPIOI2C_NACK 				((unsigned char)0)
#define I2CWRITE_MASK  			((unsigned char)0xFE)
#define I2CREAD_MASK   			((unsigned char)0x01)


/***************************** User define ***********************************/
#define GPIOI2C_SCL_GPIO_PORT          GPIOA
#define GPIOI2C_SCL_PIN                GPIO_Pin_5
#define GPIOI2C_SCL_BUSCLK             RCC_AHBENR_GPIOA
#define GPIOI2C_SCL_GPIO_CLK_ENABLE()  RCC_AHBPeriphClockCmd(GPIOI2C_SCL_BUSCLK, ENABLE);

#define GPIOI2C_SDA_GPIO_PORT          GPIOA
#define GPIOI2C_SDA_PIN                GPIO_Pin_4
#define GPIOI2C_SDA_BUSCLK             RCC_AHBENR_GPIOA
#define GPIOI2C_SDA_GPIO_CLK_ENABLE()  RCC_AHBPeriphClockCmd(GPIOI2C_SDA_BUSCLK, ENABLE);

/* Definition of Macros for bit operations */
#define i2c_set_scl()      { GPIOI2C_SCL_GPIO_PORT->BSRR = GPIOI2C_SCL_PIN; }
#define i2c_clear_scl()    { GPIOI2C_SCL_GPIO_PORT->BRR = GPIOI2C_SCL_PIN; }

#define i2c_set_sda()      { GPIOI2C_SDA_GPIO_PORT->BSRR = GPIOI2C_SDA_PIN; }
#define i2c_clear_sda()    { GPIOI2C_SDA_GPIO_PORT->BRR = GPIOI2C_SDA_PIN; }
#define i2c_get_sda()      ( GPIOI2C_SDA_GPIO_PORT->IDR & GPIOI2C_SDA_PIN )
#define sda_wr_control(b)  { if(b & mask) i2c_set_sda() else i2c_clear_sda(); }
#define sda_rd_control(b)  { if(i2c_get_sda() != 0) b |= mask; }
/*********************************************************************************************************/

/** @defgroup  
  * I2C transaction result
  */ 
typedef enum 
{
 GPIOI2C_XFER_LASTNACK     =    ((unsigned char)0x00),   	 	/* !< No error              */
 GPIOI2C_XFER_ADDRNACK   	=		((unsigned char)0x01) ,   	  /* !< No Device             */
 GPIOI2C_XFER_ABORTNACK  	=		((unsigned char)0x02) ,   	  /* !< NACK before last byte */
 GPIOI2C_XFER_LASTACK    =			((unsigned char)0x04) ,    	/* !< ACK last byte         */
 GPIOI2C_XFER_BUSERR   =  			((unsigned char)0x10) ,    	/* !< Bus error             */
}I2C_Trans_Type;
/* Exported functions --------------------------------------------------------*/

/**-----------------------------------------------------------------------
  * @brief  Initialize the IIC GPIO
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void GPIOI2C_Bus_Init(void);
/**-----------------------------------------------------------------------
  * @brief  Send I2C start condition
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void I2C_Start(void);
/**-----------------------------------------------------------------------
  * @brief  Send I2C repeated start condition
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void I2C_reStart(void);
/**-----------------------------------------------------------------------
  * @brief  Send I2C stop condition
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void I2C_Stop(void);
/**-----------------------------------------------------------------------
 * @brief  Transmit one byte via I2C
 * @param  b Byte to send
 * @retval Acknowledgment status (1: ACK received, 0: NACK received)
-------------------------------------------------------------------------*/
unsigned char I2C_master_write(unsigned char b);
/**-----------------------------------------------------------------------
 * @brief  Receive one byte via I2C
 * @param  ack ACK/NACK to send after receiving
 * @retval Received byte
-------------------------------------------------------------------------*/
unsigned char I2C_master_read(unsigned char ack);
/**-----------------------------------------------------------------------
  * @brief  MC no parameter instruction transfer function (for sending only instructions, such as temperature measurement and capacitance conversion)
  * @param  DeviceAddr  address  *Buff  Data storage array  Size  Total length of data sent
  * @retval Whether the transmission was successful
-------------------------------------------------------------------------*/
unsigned char write_i2c(unsigned char DeviceAddr, unsigned char RegAddr, unsigned char Data);
/* Application assure size>=1. */
/**-----------------------------------------------------------------------
  * @brief  Data receiving function
  * @param  DeviceAddr  address  *Buff  Data storage array  Size  Total length of data sent
  * @retval Whether the transmission was successful
-------------------------------------------------------------------------*/
unsigned char read_i2c(unsigned char DeviceAddr, MC1081_REG RegAddr,unsigned char *pData,unsigned char size);


#endif

/********************************************** (C) Copyright MySentech **********************************************/

